<?php
class NewCenter extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('newcenter_model');
		$this->load->model('getnew_model');
		$this->load->library('session');
		if(isset($_SESSION['wechat']))
		{
			$_SESSION['id'] = $this->getnew_model->getUserId();
			$_SESSION['phone'] = $this->newcenter_model->getPhone();
		}
		else
		{
			exit('非法访问，请从微信进入');
		}
	}

	/*public function index()
	{
		$data = array();
		if(!isset($_SESSION['id']))//判断是否为正常进入，若非正常进入则滚回首页（可能性不大）
		{
			$data['flag'] = 0;
		}
		else
		{
			$data['flag'] = 1;
		}

		if($this->newcenter_model->getDetailState())//判断是否完善用户资料
		{
			$data['state'] = 1;
		}
		else
		{
			$data['state'] = 0;
		}
		$data['page_id'] = 3;
		$this->load->view('new_newcenter/index',$data);
	}*/
/*
	public function detail()
	{
		$data = array();
		
		if($this->newcenter_model->getDetailState())//判断是否完善用户资料
		{
			$data['state'] = 1;
			$data['detail'] = $this->newcenter_model->getDetailAll();
		}
		else
		{
			$data['state'] = 0;
		}
		$this->load->view('new_newcenter/detail',$data);
	}
*/
    public function saveDetail()
	{
		$data = $this->input->post();
		$result = array();
		$count = 0;
		$returnBuffuer = null;
		foreach($data as  $r=>$v)
		{
			if(empty($v) && $r!='phone' && $r!='code')
			{
				$result[]=$r;
				$count++;
			}
		}//验空
		$phone_flag = ($data['phone'] == $_SESSION['phone'] && isset($data['phone']) && $_SESSION['phone'] != 'null') ? true:false;
		//检查手机号，确保提交上来的和SESSINO中的一致；
		if($count !=0 )
		{
		   echo json_encode($result);eixt;
		}
		else
		{
			if((!isset($_SESSION['code']) || $_SESSION['code'] != $data['code']) && !$phone_flag )
			{
				$returnBuffuer = array("flag" => "code_wrong");
			}
			if(!isset($_SESSION['phone']) && !$phone_flag)
			{
				$returnBuffuer = array("flag"=>'phone_unmark');
			}
			$this->newcenter_model->updateDetail($data);
			$returnBuffuer = array('flag'=>'success','state'=>0);
		}
		echo json_encode($returnBuffuer);
	}
	public function getState()
	{
		if($this->newcenter_model->getDetailState())
		{
			echo json_encode($this->newcenter_model->getDetailAll());
		}
		else
		{
			echo json_encode(array('flag'=>'0'));
		}
	}
	
	public function getAvatar()
	{
		echo json_encode(array($_SESSION['avatar']));
	}
	
	public function getIfDetailed()
	{
		echo json_encode($this->newcenter_model->getDetailState());
	}
}